package com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.BitAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName song
 * @Description TODO
 * @Author ding
 * @Date 2021/9/12 16:43
 * @Version 1.0
 **/
public class BitAutoWebDelearCrawlerScheduleJob extends BitAutoCommonCrawlerSchedulerJob {
    private static final String crawler_level = "normal";

    public static void main(String[] args) {
        publishBitAutoCrawlerSchedulerJobInfo();
    }

    public static CrawlerJob publishBitAutoCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = bitAutoScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("易车车型数据实时采集任务");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }
    public static CrawlerJob bitAutoScheduler(){

        //车型信息采集
        CrawlerRequestRecord articleCrawler = carTypeCrawler();
        //经销商采集
        String site = articleCrawler.tagsCreator().bizTags().site();
        String site_bize = articleCrawler.tagsCreator().bizTags().siteBiz();

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/bitauto/keys?site=bitCitySeries")
                .requestLabelTag(CrawlerEnum.CrawlerRequestType.supportSource)
                .requestLabelTag(CrawlerEnum.CrawlerRequestType.internalDownload)
                .build();

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//bitAutoDealer-bitAutoDealer-normal-realtime
                        StringUtils.joinWith("-",site,site_bize,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain, site, crawler_level,"queue")))
                //.kafkaResultPipeline("kafka",kafkaTopciForRealTimeJob,null)
                .fileResultPipeline("kafka","/data/chance_crawler_runner/logs/node/bitautodealer",false)
                .crawlerJobThreadNumber(30)
                .requestRecord(articleCrawler)
                .supportRecord(keywordRecord)
                .build();
    }
    public static CrawlerRequestRecord carTypeCrawler(){
        String site = "bitAutoDealer";
        String site_bize = site +"-"+crawler_level;
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .recordKey("https://dealer.yiche.com/")
                .httpUrl("https://dealer.yiche.com/")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.key)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .proxy(proxy)
                .build();

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_bize);
        return requestRecord;
    }

}
